package com.kgc.itrip.service.impl;

import com.kgc.itrip.common.Constants;
import com.kgc.itrip.common.EmptyUtils;
import com.kgc.itrip.mapper.ItripHotelOrderMapper;
import com.kgc.itrip.mapper.ItripTradeEndsMapper;
import com.kgc.itrip.pojo.ItripHotelOrder;
import com.kgc.itrip.pojo.ItripTradeEnds;
import com.kgc.itrip.service.LocalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class LocalServiceImpl implements LocalService {
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Resource
    private ItripTradeEndsMapper itripTradeEndsMapper;
    @Override
    public ItripHotelOrder getItripHotelOrderByOrderNo(String orderNo) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("orderNo",orderNo);
        List<ItripHotelOrder> itripHotelOrderList = itripHotelOrderMapper.getItripHotelOrderListByMap(params);
        if(itripHotelOrderList!=null && itripHotelOrderList.size()==1){
            return itripHotelOrderList.get(0);
        }
        return null;
    }

    @Override
    public Map<String, String> getAliPayCallbackParams(HttpServletRequest request) throws Exception {
        //获取支付宝GET过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        return params;
    }

    @Override
    public synchronized boolean itriptxUpafateItripHotelOrder(String orderNo, String tradeNo, Integer payType) throws Exception {
        ItripHotelOrder itripHotelOrder = this.getItripHotelOrderByOrderNo(orderNo);
        if(EmptyUtils.isNotEmpty(itripHotelOrder)&&EmptyUtils.isNotEmpty(itripHotelOrder.getTradeNo())){
            return true;
        }
        itripHotelOrder.setOrderStatus(Constants.ORDER_STATUS.PAY_SUCCESS);
        itripHotelOrder.setPayType(payType);
        itripHotelOrder.setTradeNo(tradeNo);
        itripHotelOrder.setModifyDate(new Date());
        itripHotelOrderMapper.updateItripHotelOrder(itripHotelOrder);
        //向交易表中插入一条数据
        ItripTradeEnds ends = new ItripTradeEnds();
        ends.setId(itripHotelOrder.getId());
        ends.setOrderNo(itripHotelOrder.getOrderNo());
        Integer integer = itripTradeEndsMapper.insertItripTradeEnds(ends);
        return true;
    }
}
